<?php
namespace app\common\controller;

use app\BaseController;
use think\facade\Request;
use think\facade\View;
use think\response\Json;


class AdminController extends BaseController
{
    /**
     * @ 当前操作的模型类
     */
    protected $model;

    /**
     * @ 当前模型类的验证器
     */
    protected $validate;

    /**
     * @ 当前验证器使用场景
     */
    protected $scene;

    /**
     * @ 操作成功回调地址
     */
    protected $redict;

    public function initialize()
    {
        parent::initialize();
    }

    /**
     * 公共添加方法
     * @return \think\response\View
     */
    public function add(){
        $view = [
            'info' => '',
            'title' => lang('add'),
        ];
        View::assign($view);
        return view();
    }
    /**
     * 公共新增方法
     * @return Json
     */
    public function create(){
        $data = $this->request->post();
        $res = $this->model->createData($data,$this->validate,$this->scene);
        if ($res['code'] == 0){
            $this->error($res['msg']);
        }
        $this->success($res['msg'],$this->redict);
    }

    /**
     * 公共只读方法
     * @return \think\response\View
     */
    public function read($tpl = ''){
        $id = Request::get('id');
        $info = $this->model->find($id);
        $view = [
            'info' => $info,
            'title' => lang('edit'),
        ];
        View::assign($view);
        return view($tpl);
    }
    /**
     * 公共保存方法
     * @return \think\response\View
     */
    public function modify(){
        $data = $this->request->post();
        $res = $this->model->modifyData($data,$this->validate,$this->scene);

        if ($res['code'] == 0){
            $this->error($res['msg']);
        }else{
            $this->success($res['msg'], $this->redict);
        }
    }

    /**
     * 公共设置状态的方法
     * 必须要保证提交的字段为id,field
     */
    public function state(){
        $id = $this->request->post('id');
        if($id){
            $data = $this->request->post();
            $info = $this->model->find($id);
            $field = $data['field'];
            $status = $info->$field==1?0:1;
            $this->model->where('id',$id)->update([$field=>$status]);
            if (isset($info->pid)){
                $ids = getAuthChildIds($this->model,$info->id);
                $this->model->where('id','in',$ids)->update([$field=>$status]);
            }
            $this->success(lang('edit success'));
        }else{
            $this->error(lang('invalid data'));
        }
    }
    /**
     * 公共设置排序的方法
     * 必须要保证提交的字段为id,field
     */
    public function order(){
        $id = $this->request->param('id');
        $sort = $this->request->param('sort');
        $info = $this->model->find($id);
        $info->sort = $sort;
        $info->save();
        $this->success(lang('edit success'));
    }
    /**
     * 公共删除方法
     * 必须要保证提交的字段为ids
     */
    public function del(){
        $ids = $this->request->post('ids');
        if($ids){
            $this->model->where('id','in',$ids)->delete();
            $this->success(lang('delete success'));
        }else{
            $this->error(lang('invalid data'));
        }
    }

}
